export async function saveImageToPhotosAlbum(url: string) {
    if (!url) return uni.$u.toast('图片错误')
    //#ifdef H5
    uni.$u.toast('长按图片保存')
    //#endif
    //#ifndef H5
    try {
        const res: any = await uni.downloadFile({ url, timeout: 10000 })
        await uni.saveImageToPhotosAlbum({
            filePath: res.tempFilePath
        })
        uni.showToast({
            title: '保存成功',
            icon: 'success'
        })
    } catch (error: any) {
        uni.$u.toast(error.errMsg || '保存失败')
    }
    //#endif
}

export async function downFileFun(url: string) {
    uni.showLoading({ title: '正在下载...' })
    uni.downloadFile({
        url: url,
        success: (data) => {
            console.log(data)
            if (data.statusCode === 200) {
                uni.hideLoading()
                uni.saveFile({
                    tempFilePath: data.tempFilePath,
                    success: (res) => {
                        uni.showToast({
                            icon: 'success',
                            mask: true,
                            title: '下载成功',
                            duration: 2000
                        })
                        setTimeout(() => {
                            const filePath = res.savedFilePath
                            uni.openDocument({
                                filePath: filePath,
                                success: () => {
                                    console.log('打开成功')
                                },
                                fail: (err) => {
                                    console.log('打开失败=>', err)
                                }
                            })
                        }, 1000)
                    }
                })
            }
        }
    })
}
